package com.quad.innovators.salesease.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonValue;
import com.quad.innovators.salesease.exception.BusinessException;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;

/**
 * @author cheng
 */
@Getter
public enum TaskAddressTypeEnum implements IBaseEnum<Integer>{

    WAREHOUSE(1, "仓库"),
    SHOP(2, "门店");

    //  Mybatis-Plus 提供注解表示插入数据库时插入该值
    @EnumValue
    private final Integer value;

    @JsonValue //  表示对枚举序列化时返回此字段
    private final String label;

    TaskAddressTypeEnum(Integer value, String label) {
        this.value = value;
        this.label = label;
    }

    static final Map<Integer, TaskAddressTypeEnum> map = new HashMap<>();

    static {
        TaskAddressTypeEnum[] values = values();
        for (TaskAddressTypeEnum value : values) {
            map.put(value.value, value);
        }
    }

    public static TaskAddressTypeEnum useCodeGetEnum(Integer code) {
        TaskAddressTypeEnum taskAddressTypeEnum = map.getOrDefault(code, null);
        if (ObjectUtils.isNull(taskAddressTypeEnum)){
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return taskAddressTypeEnum;
    }
}
